<?php
namespace Home\Controller;

use Common\Controller\HomeBaseController;
use Think\Verify;

/**
 * 首页控制器
 */
class RegisterController extends HomeBaseController
{
    /**
     * 首页
     */
    public function reg()
    {
        $this->assign('curr', 'reg');
        $this->assign('title', '开户注册-皇彩鸿娱乐平台');
        $this->display('reg');
    }
    public function registration()
    {
        if (IS_POST) {
            $parent_id = $_SESSION['parent_id'];
            $code      = I('code');
            if ($this->check_verify1($code)) {
                $User                 = M('Member');
                $map['member_mobile'] = I('mobile');
                $map1['member_name']  = I('username');
                $count                = $User->where($map)->count();
                $count1               = $User->where($map1)->count();
                if (!$count1) {
                    if (!$count) {
                        $data = array(
                            'member_name'     => I('username'),
                            'member_password' => md5(I('password')),
                            'member_mobile'   => I('mobile'),
                            'is_bind_mobile'  => 1,
                            'wechat'          => I('wechat'),
                            'add_time'        => time(),
                        );
                        if ($parent_id) {
                            $data['parent_id'] = $parent_id;
                            $data['is_agent']  = 1;
                        }
                        if ($User->add($data)) {
                            echo json_encode(array('success' => 1, 'message' => '注册成功！'));
                        } else {
                            echo json_encode(array('error' => 0, 'message' => '注册失败！'));
                        }
                    } else {
                        echo json_encode(array('error' => 0, 'message' => '该手机号码已注册！'));
                    }
                } else {
                    echo json_encode(array('error' => 0, 'message' => '用户名已存在！'));
                }

            } else {

                echo json_encode(array('error' => 0, 'message' => '验证码错误！'));
            }
        }

    }

    //生成验证码
    /*
     * codeSet指定验证码字符集的参数 要在tp3.2.1以上的版本才生效
     * 现使用的是3.2.0版本  所以是无效的
     *  */
    public function verify()
    {
        ob_clean();
        $config = array(
            'codeSet'  => '1234567890',
            'fontSize' => 9,
            'useCurve' => false,
            'useNoise' => false,
            'imageH'   => 24,
            'imageW'   => 68,
            'length'   => 4,
            'fontttf'  => '4.ttf',
        );

        $Verify = new Verify($config);

        $Verify->entry(1);
    }

    /* 忘记密码 */

    public function forget_pwd()
    {
        $this->assign('curr', 'forget');
        $this->assign('title', '找回密码-皇彩鸿娱乐平台');
        $this->display('find-pwd');
    }

    public function forget_password()
    {

        if (IS_POST) {
            $username = I('username');
            $mobile   = I('mobile');

            $User                 = M('Member');
            $map['member_name']   = $username;
            $map['member_mobile'] = $mobile;
            $code                 = getRandStr(4);
            session('code', $code);
            $count = $User->where($map)->count();
            if ($count) {

                echo json_encode(array('success' => 1, 'message' => "您的验证码是" . $code));

            } else {
                echo json_encode(array('error' => 0, 'message' => "用户名不存在或绑定的手机号错误"));
            }

        }

    }

    public function fotGetPassword()
    {
        if (IS_POST) {
            $username  = I('name');
            $code      = I('code');
            $checkcode = session('code');
            $data      = array(
                'member_password' => md5(I('password')),
            );
            if ($code == $checkcode) {
                $map['member_name'] = $username;
                $User               = M('Member');
                if ($User->where($map)->save($data)) {
                    echo json_encode(array("success" => 1, "message" => "修改成功"));
                } else {
                    die(json_encode(array("error" => 0, "message" => "修改失败")));
                }

            } else {
                echo json_encode(array('error' => 0, 'message' => "验证码错误！"));
            }
        }

    }

    //检测验证码
    public function check_verify1($code, $id = 1)
    {
        $Verify        = new \Think\Verify();
        $Verify->reset = false;
        return $Verify->check($code, $id);

    }

}
